<?php

//接收dz通知加密
class DiscuzController extends FrontBaseC {

    //同步登录
    public function actionLogin() {
        $sign = Yii::app()->request->getParam('sign');
        if (empty($sign)) {
            return true;
        }
        $sign = Encrypt::authcode($sign, 'DECODE', Yii::app()->params['bbsInfo']['key']);
        if (empty($sign)) {
            return true;
        }
        $data = json_decode($sign, true);
        if (empty($data)) {
            return true;
        }
        $userInfo = Yii::app()->user->getLocalUser($data['username']);
        Yii::app()->user->registerLogin($userInfo);
        return true;
    }

    //退出登录
    public function actionLogout() {
        Yii::app()->user->logout();
        return true;
    }

    //更新密码和基本资料
    public function actionPassword() {
        $sign = Yii::app()->request->getParam('sign');
        if (empty($sign)) {
            $this->ajaxReturn(array('status' => false, 'error' => '解密失败！'));
        }
        $sign = Encrypt::authcode($sign, 'DECODE', Yii::app()->params['bbsInfo']['key']);
        if (empty($sign)) {
            $this->ajaxReturn(array('status' => false, 'error' => '解密失败！'));
        }
        $data = json_decode($sign, true);
        if (empty($data) || empty($data['username'])) {
            $this->ajaxReturn(array('status' => false, 'error' => '用户名等信息不能为空！'));
        }
        $model = MembersModel::model();
        $userInfo = $model->find($model->where(array('username' => $data['username'])));
        if (empty($userInfo)) {
            $this->ajaxReturn(array('status' => false, 'error' => '该用户不存在！'));
        }
        //修改密码
        if ($data['new_password'] && $data['new_password'] != $data['old_password']) {
            $userInfo->password = $model->hashPassword($data['new_password']);
        }
        //修改邮箱
        if ($data['email'] && $userInfo->email != $data['email']) {
            $userInfo->email = $data['email'];
        }
        if ($userInfo->validate() && $userInfo->save()) {
            $this->ajaxReturn(array('status' => true, 'error' => '修改成功！'));
        } else {
            $this->ajaxReturn(array('status' => false, 'error' => $userInfo->getOneError() ? $userInfo->getOneError() : '该用户不存在！'));
        }
    }

    //根据用户名密码获取用户资料
    public function actionUserinfo() {
        $sign = Yii::app()->request->getParam('sign');
        if (empty($sign)) {
            $this->ajaxReturn(array('status' => false, 'error' => '解密失败！'));
        }
        $sign = Encrypt::authcode($sign, 'DECODE', Yii::app()->params['bbsInfo']['key']);
        if (empty($sign)) {
            $this->ajaxReturn(array('status' => false, 'error' => '解密失败！'));
        }
        $data = json_decode($sign, true);
        if (empty($data) || empty($data['username'])) {
            $this->ajaxReturn(array('status' => false, 'error' => '用户名等信息不能为空！'));
        }
        $userInfo = Yii::app()->user->getLocalUser($data['username'], $data['password']);
        if (empty($userInfo)) {
            $this->ajaxReturn(array('status' => false, 'error' => '该用户不存在或者帐号密码错误！'));
        }
        //明文密码传回去
        $userInfo['password'] = $data['password'];
        $this->ajaxReturn(array('status' => true, 'data' => $userInfo));
    }

}
